# -*- coding:utf-8 -*-
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import tostring

def dict_to_xml(tag,d):
	'''
	turn a simple dict into XML
	'''
	#生成一个新标签
	elem = Element(tag)
	for key,val in d.items():
		#生成一个子标签
		child = Element(key)
		#给子标签赋值
		child.text = str(val)
		#将子标签添加到父标签
		elem.append(child)
	#返回父标签
	return elem
		
if __name__ == "__main__":
	s = { 'name' : 'GOOG' , 'shares' : 100, 'price' : 490.1 }
	e = dict_to_xml('stock',s)
	print(e)
	#tostring :Generate string representation of XML element.
	print(tostring(e,encoding='UTF-8'))
	#set: Set element attribute
	e.set('_id','1234')
	print(tostring(e))